<template>
  <div class="xtx-city">
    <div class="select" @click.stop="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!changeResult.provinceName">请选择配送地址</span>
      <span
        v-else
        class="value"
      >{{ changeResult.provinceName }} {{ changeResult.cityName }} {{ changeResult.countyName }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" @click.stop="celectFn(i)" v-for="i in cityList" :key="i">{{ i.name }}</span>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { ref, reactive } from 'vue'
export default {
  name: 'XtxCity',
  setup (props, { emit }) {
    document.addEventListener('click', function () {
      if (active.value) {
        active.value = !active.value
      }
    })
    const active = ref(false)
    const cityList = ref([])
    // 存储城市数据
    let cacheList = []

    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '' // 地区名
    })

    // 显示隐藏地址盒子
    function toggleDialog () {
      getList()

      if (active.value) {
        cityList.value = cacheList
        changeResult.provinceCode = ''
        changeResult.provinceName = ''
        changeResult.cityCode = ''
        changeResult.cityName = ''
        changeResult.countyCode = ''
        changeResult.countyName = ''
      }
    }

    // 获取数据
    async function getList () {
      if (!cacheList.length) {
        const res = await axios.get('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
        cityList.value = res.data
        cacheList = res.data
      }

      active.value = !active.value
    }

    // 选择城市
    function celectFn (i) {
      cityList.value = i.areaList
      if (i.level === 0) {
        changeResult.provinceCode = i.code
        changeResult.provinceName = i.name
      }
      if (i.level === 1) {
        changeResult.cityCode = i.code
        changeResult.cityName = i.name
      }
      if (i.level === 2) {
        changeResult.countyCode = i.code
        changeResult.countyName = i.name
        active.value = false

        // 子传父地址
        emit('getAddress', changeResult)
      }
    }

    return {
      active,
      toggleDialog,
      cityList,
      cacheList,
      celectFn,
      changeResult

    }
  }
}

</script>
<style scoped lang="less">
.clickBox {
  position: absolute;
  width: 100%;
  height: 36%;
  top: 0;
  left: 0;
}
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
